"""
@file Senior.py
@author GalaktiK (jacobliu001@qq.com)
@version 1.0
@date 2021-04-25

@copyright Copyright (c) 2021, Published under License GPLv2
"""

# a helper constant for the 8-connected neighbours
# to easily access the coordinates of the neighbours,
# simply add each element of this to the original coordinates.
dxy = ((1,0),(-1,0),(0,1),(0,-1),
       (1,1),(-1,1),(-1,-1),(1,-1))

# simple input processing
a, b = map(int, input().split())

n = int(input())

# The board-storing technique I used is a 3d array (NumPy is not allowed in this contest)
# The first dimension is the x-coordinate, the second the y-coordinate
# A 1-d array is stored at each point in board[x][y]. This array stores the value in each
# original matrix given in the problem statement (in order).
board = [[[] for _ in range(b)] for _ in range(a)]

# to be honest this input processing can be improved memory-wise.
for _ in range(n):
    ss = list(map(int,input().split()))
    for pos in range(a*b):
        board[pos//b][pos%b].append(ss[pos])

def getMax(cx, cy):
    """gets the position of the maximum neighbour block.

    Args:
        cx (int): the current x-coordinate
        cy (int): the current y-coordinate

    Returns:
        tuple[int, int]: the x-,y-coordinates of the maximum neighour.
    """
    def inRange(x, y):
        """Checks if a point is in range of the board.

        Args:
            x (int): x-coordinate of the point to check
            y (int): y-coordinate of the point to check

        Returns:
            bool: if the point (x,y) is in bounds within the board.
        """
        return ((0 <= x < a) and (0 <= y < b))
    vals = []
    ddxy = []
    # enumerates through all neighbours.
    # the vals list stores all values of all legal neighbours.
    # ddxy stores the dx and dy of all neighbours in order so
    # that after the value is found, the position of the desired
    # neighbour can be easily addressed by dividing the position
    # by the number of values per cell. (The number of rectangles)
    for pxy in dxy:
        dx, dy = pxy
        nx, ny = cx+dx, cy+dy
        if not inRange(nx, ny):
            continue
        ddxy.append(pxy)
        for val in board[nx][ny]:
            vals.append(val)
    maxVal = 0
    # Here we get the value that is the maximum unique value.
    # We first sort the values in descending order,
    # and then check each one of the values to see if there
    # is only one. If there is only one, break out of the loop
    # and store the value as maxVal.
    ## It is obvious that there are better ways to
    ## implement all of this but time was not something I
    ## had and this was the most straightforward for me.
    for val in sorted(vals, reverse=True):
        if vals.count(val) == 1:
            maxVal = val
            break
    posVal = vals.index(maxVal)
    # recover the position from the index of posVal.
    dx, dy = ddxy[posVal//n]
    return (cx+dx, cy+dy)

# prevPos is a list of tuples (x,y) to record the path taken.
cx, cy = 0, 0
prevPos = []
while True:
    # It adds the position to the path recorder list
    prevPos.append((cx, cy))
    nx, ny = getMax(cx, cy)
    # checks if the new position is already taken
    if (nx, ny) in prevPos:
        break
    # if not taken, move to the next position.
    cx, cy = nx, ny

# This might need a bit of explaining:
# Lambda expressions can be interpreted as a normal function f(x)
# For instance, if we have a proper function (needs to be pure)
# def f(x):
#     return x+1
# and the lambda expression
# g = lambda x: x+1
# running f(x) and g(x) essentially are equivalent
# (that is, provided that you don't have very peculiar overloaded + operations)
# The following code can be refractored to be more explicit:
# s = 0
# for x in prevPos:
#     s += min(board[x[0]][x[1]])
# print(s)
# I like python one-liners xD
# Also, if you don't know what map(func, iterable) means,
# if the iterable is [x1, x2, x3,...]
# map(func, iterable) returns an iterable that equals [func(x1), func(x2), func(x3), ...]
# so the one-liner does this:
# take prevPos, get each element x, which is a tuple storing the position,
# retrieve information about that position on the board, and takes the
# appropriate value (the minimum among the N values at that location),
# sum values extracted for each element x
print(sum(map(lambda x: min(board[x[0]][x[1]]), prevPos)))
